/**
  ******************************************************************************
  * @file    psmc.h
  * @author  ashuai0110
  * @version V1.0
  * @date    2024-01-24
  * @brief   psmc驱动,适用于PIC16(L)F1788_9
  *
  ******************************************************************************
  * @attention
  *
  ******************************************************************************
  */

#ifndef __PSMC_H
#define __PSMC_H

#ifdef __cplusplus
extern "C" {
#endif

/* 包含头文件-----------------------------------------------------------------*/
#include "common.h"

/* 宏定义---------------------------------------------------------------------*/
/* psmc_channel PSMC输出通道选择 */
#define PSMC_CH_CLOSE                       (0x0)  /* 输出通道关闭恢复IO功能 */
#define PSMC_CH_A                           (0x01)
#define PSMC_CH_B                           (0x02)
#define PSMC_CH_C                           (0x04)
#define PSMC_CH_D                           (0x08)
#define PSMC_CH_E                           (0x10)
#define PSMC_CH_F                           (0x20)
    
#define PSMC_CNT_MAX                        (0xFFFF)
    
/* PSMCx开关控制 0:关闭  1:开启  */
#define PSMC1_CMD(x)                        (PSMC1LD = (x), PSMC1EN = (x))
#define PSMC2_CMD(x)                        (PSMC2LD = (x), PSMC2EN = (x))
#define PSMC3_CMD(x)                        (PSMC3LD = (x), PSMC3EN = (x))
#define PSMC4_CMD(x)                        (PSMC4LD = (x), PSMC4EN = (x))
/* PSMCx时基计数值设置 0~0xFFFF */
#define PSMC1_SET_TMR_CNT(val)              (PSMC1TMRL = ((val) & 0xFF), PSMC1TMRH = ((val) >> 8))
#define PSMC2_SET_TMR_CNT(val)              (PSMC2TMRL = ((val) & 0xFF), PSMC2TMRH = ((val) >> 8))
#define PSMC3_SET_TMR_CNT(val)              (PSMC3TMRL = ((val) & 0xFF), PSMC3TMRH = ((val) >> 8))
#define PSMC4_SET_TMR_CNT(val)              (PSMC4TMRL = ((val) & 0xFF), PSMC4TMRH = ((val) >> 8))
/* PSMCx相位(上升沿事件)计数值设置 0~0xFFFF */
#define PSMC1_SET_RISE_CNT(val)             (PSMC1PHL = ((val) & 0xFF), PSMC1PHH = ((val) >> 8))
#define PSMC2_SET_RISE_CNT(val)             (PSMC2PHL = ((val) & 0xFF), PSMC2PHH = ((val) >> 8))
#define PSMC3_SET_RISE_CNT(val)             (PSMC3PHL = ((val) & 0xFF), PSMC3PHH = ((val) >> 8))
#define PSMC4_SET_RISE_CNT(val)             (PSMC4PHL = ((val) & 0xFF), PSMC4PHH = ((val) >> 8))
/* PSMCx占空比(下降沿事件)计数值设置 0~0xFFFF */
#define PSMC1_SET_FALL_CNT(val)             (PSMC1DCL = ((val) & 0xFF), PSMC1DCH = ((val) >> 8))
#define PSMC2_SET_FALL_CNT(val)             (PSMC2DCL = ((val) & 0xFF), PSMC2DCH = ((val) >> 8))
#define PSMC3_SET_FALL_CNT(val)             (PSMC3DCL = ((val) & 0xFF), PSMC3DCH = ((val) >> 8))
#define PSMC4_SET_FALL_CNT(val)             (PSMC4DCL = ((val) & 0xFF), PSMC4DCH = ((val) >> 8))
/* PSMCx周期计数值设置 0~0xFFFF */
#define PSMC1_SET_PERIOD_CNT(val)           (PSMC1PRL = ((val) & 0xFF), PSMC1PRH = ((val) >> 8))
#define PSMC2_SET_PERIOD_CNT(val)           (PSMC2PRL = ((val) & 0xFF), PSMC2PRH = ((val) >> 8))
#define PSMC3_SET_PERIOD_CNT(val)           (PSMC3PRL = ((val) & 0xFF), PSMC3PRH = ((val) >> 8))
#define PSMC4_SET_PERIOD_CNT(val)           (PSMC4PRL = ((val) & 0xFF), PSMC4PRH = ((val) >> 8))
/* PSMCx产生的中断控制(类似全局中断控制,它控制PSMCx下的所有中断) 0:关闭 1:开启 */
#define PSMC1_IT_CMD(x)                     (PSMC1TIE = (x))
#define PSMC2_IT_CMD(x)                     (PSMC2TIE = (x))
#define PSMC3_IT_CMD(x)                     (PSMC3TIE = (x))
#define PSMC4_IT_CMD(x)                     (PSMC4TIE = (x))
/* PSMCx时基溢出中断控制 0:关闭 1:开启 */
#define PSMC1_TMR_IT_CMD(x)                 (P1TOVIE = (x))
#define PSMC2_TMR_IT_CMD(x)                 (P2TOVIE = (x))
#define PSMC3_TMR_IT_CMD(x)                 (P3TOVIE = (x))
#define PSMC4_TMR_IT_CMD(x)                 (P4TOVIE = (x))
/* PSMCx相位(上升沿事件)中断控制 0:关闭 1:开启 */
#define PSMC1_RISE_IT_CMD(x)                (P1TPHIE = (x))
#define PSMC2_RISE_IT_CMD(x)                (P2TPHIE = (x))
#define PSMC3_RISE_IT_CMD(x)                (P3TPHIE = (x))
#define PSMC4_RISE_IT_CMD(x)                (P4TPHIE = (x))
/* PSMCx占空比(下降沿事件)中断控制 0:关闭 1:开启 */
#define PSMC1_FALL_IT_CMD(x)                (P1TDCIE = (x))
#define PSMC2_FALL_IT_CMD(x)                (P2TDCIE = (x))
#define PSMC3_FALL_IT_CMD(x)                (P3TDCIE = (x))
#define PSMC4_FALL_IT_CMD(x)                (P4TDCIE = (x))
/* PSMCx周期中断控制 0:关闭 1:开启 */
#define PSMC1_PERIOD_IT_CMD(x)              (P1TPRIE = (x))
#define PSMC2_PERIOD_IT_CMD(x)              (P2TPRIE = (x))
#define PSMC3_PERIOD_IT_CMD(x)              (P3TPRIE = (x))
#define PSMC4_PERIOD_IT_CMD(x)              (P4TPRIE = (x))
/* PSMCx时基溢出中断标志位 */
#define PSMC1_TMR_IT_FLAG                   P1TOVIF
#define PSMC2_TMR_IT_FLAG                   P2TOVIF
#define PSMC3_TMR_IT_FLAG                   P3TOVIF
#define PSMC4_TMR_IT_FLAG                   P4TOVIF
/* PSMCx相位(上升沿事件)中断标志位 */
#define PSMC1_RISE_IT_FLAG                  P1TPHIF
#define PSMC2_RISE_IT_FLAG                  P2TPHIF
#define PSMC3_RISE_IT_FLAG                  P3TPHIF
#define PSMC4_RISE_IT_FLAG                  P4TPHIF
/* PSMCx占空比(下降沿事件)中断标志位 */
#define PSMC1_FALL_IT_FLAG                  P1TDCIF
#define PSMC2_FALL_IT_FLAG                  P2TDCIF
#define PSMC3_FALL_IT_FLAG                  P3TDCIF
#define PSMC4_FALL_IT_FLAG                  P4TDCIF
/* PSMCx周期中断标志位 */
#define PSMC1_PERIOD_IT_FLAG                P1TPRIF
#define PSMC2_PERIOD_IT_FLAG                P2TPRIF
#define PSMC3_PERIOD_IT_FLAG                P3TPRIF
#define PSMC4_PERIOD_IT_FLAG                P4TPRIF
/* PSMCx通道输出使能控制 置位的通道作为psmc输出否则是普通io @ref psmc_channel */
#define PSMC1_OUT_EN_CTRL(ch)               (PSMC1OEN = (ch))
#define PSMC2_OUT_EN_CTRL(ch)               (PSMC2OEN = (ch))
#define PSMC3_OUT_EN_CTRL(ch)               (PSMC3OEN = (ch))
#define PSMC4_OUT_EN_CTRL(ch)               (PSMC4OEN = (ch))
/* PSMCx通道输出转向控制 置位的通道可以输出PWM否则输出无效 @ref psmc_channel */
#define PSMC1_OUT_SW_CTRL(ch)               (PSMC1STR0 = (ch))
#define PSMC2_OUT_SW_CTRL(ch)               (PSMC2STR0 = (ch))
#define PSMC3_OUT_SW_CTRL(ch)               (PSMC3STR0 = (ch))
#define PSMC4_OUT_SW_CTRL(ch)               (PSMC4STR0 = (ch))
    
/* 类型定义-------------------------------------------------------------------*/

/* 全局变量-------------------------------------------------------------------*/

/* 函数原型-------------------------------------------------------------------*/
void psmc1_init(uint8_t ch);


#ifdef __cplusplus
}
#endif

#endif /* psmc.h */